package sadmin

import (
	"errors"
	"github.com/astaxie/beego/orm"
	"math/rand"
	"shop/models/common"
	"time"
)

type Company struct {
	Id int
	Cname string
	Ctel string
	Caddr string
	Cemail string
	Ccode string
	Ccompanycode string
}

func init()  {
	orm.RegisterModel(new(Company))
}

func CompanyAdd(company Company) (int64,error) {
	company.Cname=common.StrDelTrim(company.Cname)
	company.Ctel=common.StrDelTrim(company.Ctel)
	company.Caddr=common.StrDelTrim(company.Caddr)
	company.Cemail=common.StrDelTrim(company.Cemail)
	company.Ccode=common.StrDelTrim(company.Ccode)
	if (company.Cname==""||company.Ctel==""||company.Caddr==""||company.Cemail==""||company.Ccode==""){
		return 0,errors.New("can't be null")
	}else {
		n:=common.StrDelTrim(company.Cname)
		_,err:=findByName(n)
		if err==nil{
			return 0,errors.New("company exists")
		}else {
			o:=orm.NewOrm()
			company.Ccompanycode=randnum()
			return o.Insert(&company)
		}
	}

}

func CompanyShow(name string,code string) ([]Company, error) {
	o:=orm.NewOrm()
	var c []Company
	com:=new(Company)
	qs:=o.QueryTable(com)
	_,err:=qs.Filter("cname__icontains",common.StrDelTrim(name)).Filter("ccode__icontains",common.StrDelTrim(code)).All(&c)
	return c,err
}

func CompanyShowById(id int) Company {
	com,_:=findById(id)
	return com
}

//user use
func AllCompany() ([]string) {
	o:=orm.NewOrm()
	var c []Company
	com:=new(Company)
	qs:=o.QueryTable(com)
	_,_=qs.All(&c,"cname")
	var st []string
	for i:=0;i<len(c);i++{
		st=append(st,c[i].Cname)
	}
	return st
}
func CompanyDel(id int) error {
	com,err:=findById(id);
	if err!=nil{
		return errors.New("don't  exists")
	}else {
		//查找该企业下是有有用户
		if findCompany(com.Cname)==nil{
			return errors.New("have users ,can't delete")
		}
		o:=orm.NewOrm()
		_,err=o.Delete(&Company{Id:id})
		return  err
	}
}

func CompanyEdit(u Company)error  {
	o := orm.NewOrm()
	user := Company{Id:u.Id}
	if o.Read(&user) == nil {
		user.Ctel=common.StrDelTrim(user.Ctel)
		user.Caddr=common.StrDelTrim(user.Caddr)
		user.Cemail=common.StrDelTrim(user.Cemail)
		user.Ccode=common.StrDelTrim(user.Ccode)
		n:=common.StrDelTrim(u.Cname)
		if n!=""{
			//名字变了并且不存在进行修改名字
			//判断
			_,err:=findByName(n)
			if err==nil{
				//存在，也可能是自己
				if n==user.Cname{
					//名字没变，do nothing
				}else {
					return errors.New("company exists")
				}
			}else {
			user.Cname=u.Cname
			}
		}else {
			return errors.New("can't be null")
		}
		if u.Ctel!=""{
			user.Ctel=u.Ctel
		}else {
			return errors.New("can't be null")
		}
		if u.Caddr!=""{
			user.Caddr=u.Caddr
		}else {
			return errors.New("can't be null")
		}
		if u.Cemail!=""{
			user.Cemail=u.Cemail
		}else {
			return errors.New("can't be null")
		}
		if u.Ccode!=""{
			user.Ccode=u.Ccode
		}else {
			return errors.New("can't be null")
		}
		if _, err := o.Update(&user); err == nil {
			return nil
		}
		return nil
	}else {
		return errors.New("don't exist")
	}
}

func findByName(name string)(Company,error){
	o:=orm.NewOrm()
	com:=Company{Cname:name}
	err:=o.Read(&com,"cname")
	return com,err
}

func findById(id int)(Company,error){
	o:=orm.NewOrm()
	com:=Company{Id:id}
	err:=o.Read(&com,"id")
	return com,err
}


func randnum() string {
	str := "0123456789abcdefghijklmnopqrstuvwxyz"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < 6; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}